//
//  OMTLocaleManager.m
//  SocialSDKDemo
//
//  Created by liu_shaoqiu on 15/6/25.
//  Copyright (c) 2015年 onemt. All rights reserved.
//

#import "OMTLocaleManager.h"
#import <OMTCoreKit/UIApplication+Extension.h>
#import "OMTResourceManager.h"

//保存SDK语言
static NSString *kUserDefaultSDKLanguage = @"com.onemt.sdk.language";


@interface OMTLocaleManager()

@property (nonatomic, strong) NSBundle *languageBundle;

@end

@implementation OMTLocaleManager

+ (instancetype)sharedInstance {
    static OMTLocaleManager *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [OMTLocaleManager new];
    });
    return instance;
}

- (id)init {
    self = [super init];
    if (self) {
        self.currentLanguage = [[NSUserDefaults standardUserDefaults] objectForKey:kUserDefaultSDKLanguage];
        if (!self.currentLanguage) {
            self.currentLanguage = [UIApplication sharedApplication].omt_currentLanguage;
        }
    }
    return self;
}

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value{
    return [self.languageBundle localizedStringForKey:key value:value table:@"Localizable"];
}

- (NSString *)deviceLanguage {
    return [UIApplication sharedApplication].omt_currentLanguage;
}

- (void)setCurrentLanguage:(NSString *)currentLanguage {
    _currentLanguage = [currentLanguage copy];
    NSString *locationFilePath = [[OMTResourceManager localeBasePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.lproj", currentLanguage]];
    _languageBundle = [NSBundle bundleWithPath:locationFilePath];
    [[NSUserDefaults standardUserDefaults] setObject:currentLanguage forKey:kUserDefaultSDKLanguage];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
